'''
    python manage.py shell
'''

from booktest.models import BookInfo, HeroInfo
from django.utils import timezone
from datetime import *

# 插入
bookobj = BookInfo()
bookobj.btitle = '水浒传'
bookobj.bpub_date = datetime(year=2016, month=5, day=23)
bookobj.save()

bookobj.delete()                        # 这里不是删除整张表, 而是删除刚才save的那条

# 查询全部
booksAll = BookInfo.objects.all()

# 查询条件
book1 = BookInfo.objects.get(pk=1)     # 这里的pk就是id, 因为id是主键(pk)
bookid = book1.id
booktitle = book1.btitle
bookdate = book1.bpub_date

# 删除
book1.delete()                         # 删除pk=1对应的book


# 一本书包含多个hero
h = HeroInfo()
h.htitle = '郭靖'
h.hgender = True
h.hcontent = '降龙十八掌'
h.hbook = book1                        # 关联
h.save()


# 获取book1中的所有hero
heroset_in_book1 = book1.heroinfo_set.all()         # 获取一对多的"多"的集合
heroNew = book1.heroinfo_set.create(hname='黄蓉', hgender=False, hcontent='打狗棍法')  # 通过"一"来创建一个"多"的成员
